/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.agh.air.bpel2bpmn.util;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;

/**
 *
 * @author paletz
 */
public class Util {
    
    public static String specialCharReplacer(String str){
        return str.replace(".", ", ").replace("RTMP", ")").replace("LTMP", "(");
    }
    
    public static String convertStreamToString(InputStream is) throws IOException {

        if (is != null) {
            Writer writer = new StringWriter();

            char[] buffer = new char[1024];
            try {
                Reader reader = new BufferedReader(
                        new InputStreamReader(is, "UTF-8"));
                int n;
                while ((n = reader.read(buffer)) != -1) {
                    writer.write(buffer, 0, n);
                }
            } finally {
                is.close();
            }
            return writer.toString();
        } else {        
            return "";
        }
    }
    

    public static InputStream stringToStream(String text) {
 
        try {
            return new ByteArrayInputStream(text.getBytes("UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }
    
    
    public static String removeWhiteCharsFromInputStream(String str) {
        String result;
        String[] tab = str.split(">");
        result = tab[0];        
                for(int i=1; i<tab.length; i++){
                    String[] splited = tab[i].split("<");
                    if(splited.length==2)
                        result = result + "><" + splited[1];
                    else
                        result = result + "><" + splited[0]; 
                    
                }
                
        return result + ">";
    }
    
    
    
    
}
